<link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css"
      rel="external nofollow">
<div id="pad-wrapper" class="new-user">

    <div class="row-fluid header">
        <h3>优惠券发放</h3>
    </div>


    <div class="row-fluid form-wrapper">
        <!-- left column -->
        <div class="span9 with-sidebar">
            <div class="container">
                <form class="new_user_form form-horizontal" action="/coupons/issue-deal" method="post" id="dataForm"
                      enctype="multipart/form-data">
                    <input type="hidden" id="coupons_id"  value="<?=$_GET['id']?>">
                    <div class="field-box"></div>
                    <div class="control-group form-group span12">
                        <label class="control-label" for="title">优惠券名称:</label>
                        <div class="controls col-lg-5">
                            <?=$coupons_info['coupons_name']?>
                        </div>
                    </div>


                    <div class="control-group form-group span12">
                        <label class="control-label" for="title">发放数量:</label>
                        <div class="controls col-lg-5">
                            <input type="text" class="form-control"  name="f_number" id="f_number"
                                   placeholder="请输入..."/>
                        </div>
                    </div>

                    <div class="control-group form-group span12">
                        <label class="control-label" for="title">会员筛选:</label>
                        <div class="controls col-lg-5">
                            <input type="text" class="form-control" name="mobile"
                                   placeholder="请输入手机号"/>

                            <input type="text" class="form-control" name="nickname"
                                   placeholder="请输入昵称"/>
                            <input type="button" id="search" value="查询" />
                        </div>

                    </div>



                    <div class="control-group form-group span12">
                        <label class="control-label" for="users_id">选择用户:</label>
                        <div class="controls col-lg-5">
                            <select  name="users_id"  multiple class="form-control" style="height: auto;">
                            </select>
                        </div>
                    </div>


                    <div class="span12 field-box"></div>
                    <div class="control-group form-group span12 actions">
                        <label class="control-label" for="" class="control-label"></label>
                        <div class="controls col-lg-5">
                            <input type="submit" class="btn-glow primary" value="提交"/>
                            <input type="reset" class="btn-glow primary" value="取消"/>
                        </div>
                    </div>


                </form>
            </div>
        </div>


        <!-- side right column -->
        <div class="span3 form-sidebar pull-right"></div>
    </div>
</div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>
<script type="text/JavaScript" src="/My97DatePicker/WdatePicker.js"></script>
<script type="text/javascript">
    //表单提交
    $('form').submit(function () {
        $(':submit').attr('disabled', 'disabled');
        var f_number=$('#f_number').val();
        var f_user_ids=get_user_ids($('select[name=users_id]'));
        if(f_number&&f_user_ids){
            $.post($(this).attr('action'), {'number':f_number,'user_ids':f_user_ids,'coupons_id':$('#coupons_id').val()}, function ($result) {
                alert($result.msg);
                if ($result.status) {
                    window.location.href = $result.url;
                } else {
                    $(':submit').removeAttr('disabled');
                }
            }, 'json');
        }else{
            alert('用户或数量不完整');
            $(':submit').removeAttr('disabled');
        }
        return false;
    });


    $(function () {
        //点击查询
        $('#search').click(function ($event) {
            var mobile=trim($('input[name=mobile]').val());
            var nickname=trim($('input[name=nickname]').val());
            if(mobile.length>0 || nickname.length>0){
                $(this).attr('disabled','disabled');
                //发送请求
                $.post('/coupons/user-list',{'mobile':mobile,'nickname':nickname},function ($data) {
                    if($data.status){
                        var temp_data=$data.msg;
                        //数据展示
                        if(temp_data.length>0){
                            var $temple='<option value=%1>mobile:%2</option>';
                            var  long_str='';
                            for(var i=0;i<temp_data.length;i++){
                                long_str+=$temple.replace(/%1/,temp_data[i].user_id).replace(/%2/,temp_data[i].mobile);
                            }
                            $('select[name=users_id]').empty().html(long_str);
                        }else{
                            alert('没有查找到符合条件的数据');
                        }
                    }
                },'json');
                $(this).removeAttr('disabled');
            }
        });
    })


    //去掉首尾空内容
    window.trim=function ($str) {
        return $str.replace(/(^\s*)|(\s*$)/gim,'');
    }


    function get_user_ids($obj){
        var  ids=new Array();
        $obj.find('option:selected').each(function(){
                ids.push($(this).val());
        });
        return ids.join(',');
    }

</script>
